package com.lovedice;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

public class ChangeName extends Activity {

	private SharedPreferences preference;
	private TextView txtPlayerOneName;
	private TextView txtPlayerTwoName;
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.change_name);
        setTitle(R.string.title_activity_change_names);
        preference = PreferenceManager.getDefaultSharedPreferences(this);
        
    	txtPlayerOneName = ((TextView)findViewById(R.id.edittext_player1));
    	txtPlayerTwoName = ((TextView)findViewById(R.id.edittext_player2));
    	
    	Bundle extras = getIntent().getExtras();
    	txtPlayerOneName.setText(extras.getString("partnerOneName"));
    	txtPlayerTwoName.setText(extras.getString("partnerTwoName"));
    }

    public void changePartnersName(View view){
    	Intent intent = new Intent();

    	String nomes[] = new String[2];
    	nomes[0] = txtPlayerOneName.getText().toString();
    	nomes[1] = txtPlayerTwoName.getText().toString();

    	CheckBox checkSalvarNomes = ((CheckBox)findViewById(R.id.salvar_nomes_check));
    	
    	SharedPreferences.Editor editor = preference.edit();
    	if (checkSalvarNomes.isChecked()){
	        editor.putString("partner_name_one", (nomes[0].equals("")? null:nomes[0]));
	        editor.putString("partner_name_two", (nomes[1].equals("")? null:nomes[1]));
    	} else {
    		editor.putString("partner_name_one", null);
    		editor.putString("partner_name_two", null);
    	}
    	editor.commit();
    	
    	intent.putExtra("returnedData", nomes);
    	setResult(RESULT_OK, intent);
    	finish();
    }
    public void cancelChangePartnerName(View view) {
    	setResult(RESULT_CANCELED);
    	finish();
    }

}
